﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net.Mail;

namespace DeliverySolution.HOME.PUBLIC
{
    public partial class ContactUs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strWatermark = "First Name *";

                txtFirstName.Text = strWatermark;
                txtFirstName.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtFirstName.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");


                strWatermark = "Last Name *";
                txtLastName.Text = strWatermark;
                txtLastName.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtLastName.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");

                strWatermark = "Company";
                txtCompany.Text = strWatermark;
                txtCompany.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtCompany.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");

                strWatermark = "Address";
                txtAddress.Text = strWatermark;
                txtAddress.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtAddress.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");

                strWatermark = "Telephone";
                txtTelephone.Text = strWatermark;
                txtTelephone.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtTelephone.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");

                strWatermark = "Fax";

                txtFax.Text = strWatermark;
                txtFax.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtFax.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");

                strWatermark = "Email *";
                txtEmail.Text = strWatermark;
                txtEmail.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtEmail.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");

                strWatermark = "Comments *";
                txtComments.Text = strWatermark;
                txtComments.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermark + "');");
                txtComments.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermark + "');");
            }

            lblSuccess.Visible = false;
            //Anthem.Manager.AddScriptForClientSideEval("addLoadEvent(initialize);");
            //Anthem.Manager.AddScriptForClientSideEval("addUnLoadEvent(GUnload);");
            //Page.RegisterStartupScript("loadpage", "<script>addLoadEvent(initialize);</script>");
            //Page.RegisterStartupScript("unloadpage", "<script>addUnLoadEvent(GUnload);</script>");
        }

        protected void clearButton_Clicked(object sender, EventArgs e)
        {
            ResetForm();
        }

        protected void submitButton_Clicked(object sender, EventArgs e)
        {
            string firstName = "", lastName = "", company = "", address = "", telephone = "", fax = "", email = "", comments = "";
            //good
            if (Page.IsValid)
            {
                //if (Session[Constants.SessionVariables.RandomNumber] == null)
                //{
                //    lblSuccess.Text = "Session has expired. Please, reload the page.";
                //    lblSuccess.Visible = true;
                //    return;
                //}
                //if (Session[Constants.SessionVariables.RandomNumber] != null && txtBoxNotSpamNumber.Text != Session[Constants.SessionVariables.RandomNumber].ToString())
                //{
                //    lblSuccess.Text = "Try again ... wrong number";
                //    lblSuccess.Visible = true;
                //    return;
                //}

                firstName = HttpUtility.HtmlEncode(txtFirstName.Text.Trim());
                lastName = HttpUtility.HtmlEncode(txtLastName.Text.Trim());
                if (txtCompany.Text.Trim() != "Company")
                {
                    company = HttpUtility.HtmlEncode(txtCompany.Text.Trim());
                }

                if (txtAddress.Text.Trim() != "Address")
                {
                    address = HttpUtility.HtmlEncode(txtAddress.Text.Trim());
                }

                if (txtTelephone.Text.Trim() != "Telephone")
                {
                    telephone = HttpUtility.HtmlEncode(txtTelephone.Text.Trim());
                }

                if (txtFax.Text.Trim() != "Fax")
                {
                    fax = HttpUtility.HtmlEncode(txtFax.Text.Trim());
                }
                email = HttpUtility.HtmlEncode(txtEmail.Text.Trim());
                comments = HttpUtility.HtmlEncode(txtComments.Text.Trim());

                if (SendEmail(firstName, lastName, company, address, telephone, fax, email, comments))
                {
                    lblSuccess.Text = "Your message has been sent. Thank you.";
                    ResetForm();
                }
                else
                {
                    lblSuccess.Text = "Your message has not been sent. Mail server is unavailable.";
                }
            }
            else//no good
            {
                lblSuccess.Text = "Some fields ae not valid.";
            }
            lblSuccess.Visible = true;
        }

        private void ResetForm()
        {
            txtFirstName.Text = "First Name *";
            txtLastName.Text = "Last Name *";
            txtCompany.Text = "Company";
            txtAddress.Text = "Address";
            txtEmail.Text = "Email *";
            txtTelephone.Text = "Telephone";
            txtFax.Text = "Fax";
            txtComments.Text = "Comments *";
            txtBoxNotSpamNumber.Text = string.Empty;

        }

        private bool SendEmail(string firstName, string lastName, string company, string address, string telephone, string fax, string email, string comments)
        {
            try
            {
                SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["SmtpServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerPort"]));
                MailMessage mail = new MailMessage(ConfigurationManager.AppSettings["EmailFrom"], ConfigurationManager.AppSettings["EmailTo"]);
                mail.Subject = "Web Site Contact Us Form Message";
                mail.IsBodyHtml = true;
                mail.Body += string.Format("<b>First Name:</b> {0}<br />", firstName);
                mail.Body += string.Format("<b>Last Name:</b> {0}<br />", lastName);
                mail.Body += string.Format("<b>Company:</b> {0}<br />", company);
                mail.Body += string.Format("<b>Address:</b> {0}<br />", address);
                mail.Body += string.Format("<b>Telephone:</b> {0}<br />", telephone);
                mail.Body += string.Format("<b>Fax:</b> {0}<br />", fax);
                mail.Body += string.Format("<b>Email:</b> {0}<br />", email);
                mail.Body += string.Format("<b>Message:</b> {0}<br />", comments);
                smtp.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                //Logger.LogMessage(ex.ToString(), "ContactUs");
                return false;
            }
        }
    }
}